package com.mofofa.maozy.components.form

import android.annotation.SuppressLint
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.data.RevObjInfoPageFieldConfig
import com.mofofa.maozy.databinding.ViewMaozyFormSelectBankBinding
import com.mofofa.maozy.extend.addMaozyHighlightMaozyStyle
import com.mofofa.maozy.extend.addMaozyTipsMaozyStyle
import com.mofofa.maozy.extend.recordMaozySelectMaozyEvent

/**
<declare-styleable name="MaozyFormSelectBank">
<attr name="cwfsbTitle" format="string" />
<attr name="cwfsbText" format="string" />
<attr name="cwfsbHint" format="string" />
<attr name="cwfsbTitleColor" format="color" />
<attr name="cwfsbTextColor" format="color" />
<attr name="cwfsbHintColor" format="color" />
<attr name="cwfsbTitleSize" format="dimension" />
<attr name="cwfsbTextSize" format="dimension" />
</declare-styleable>
 * */
class SelectBankForm : RelativeLayout,
    com.mofofa.maozy.components.form.BaseFormAction,
    BaseFormAction {

    private lateinit var vMaozyRootMaozyView: View
    lateinit var mMaozyViewMaozyBinding: ViewMaozyFormSelectBankBinding

    private var cwfsbTitleColor: Int = 0
    private var cwfsbTextColor: Int = 0
    private var cwfsbHintColor: Int = 0
    private var cwfsbTitle: String? = null
    private var cwfsbText: String? = null
    private var cwfsbHint: String? = null
    private var cwfsbTitleSize: Float = 0F
    private var cwfsbTextSize: Float = 0F

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null
    private var mMaozyFormMaozyConfig: RevObjInfoPageFieldConfig? = null
    private var mMaozyOnMaozyStateMaozyChangedMaozyListener: (Int, String) -> Unit =
        { action, content -> }

    private var mTempInputValue: String = ""
    private var mOnActionClickListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(
                    attrs,
                    R.styleable.SelectBankForm
                )
                cwfsbTitleColor = array.getColor(
                    R.styleable.SelectBankForm_cwfsbTitleColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwfsbTitle =
                    array.getString(R.styleable.SelectBankForm_cwfsbTitle)

                cwfsbTextColor = array.getColor(
                    R.styleable.SelectBankForm_cwfsbTextColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwfsbText =
                    array.getString(R.styleable.SelectBankForm_cwfsbText)

                cwfsbHintColor = array.getColor(
                    R.styleable.SelectBankForm_cwfsbHintColor,
                    ContextCompat.getColor(cxt, R.color.input_hint)
                )
                cwfsbHint =
                    array.getString(R.styleable.SelectBankForm_cwfsbHint)

                cwfsbTitleSize = array.getDimension(
                    R.styleable.SelectBankForm_cwfsbTitleSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwfsbTextSize = array.getDimension(
                    R.styleable.SelectBankForm_cwfsbTextSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                array.recycle()
            }
        }
        context?.let {
            vMaozyRootMaozyView =
                View.inflate(it, R.layout.view_maozy_form_select_bank, this)
            mMaozyViewMaozyBinding =
                ViewMaozyFormSelectBankBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.textFormSelectBankTitle.text = (cwfsbTitle)
            mMaozyViewMaozyBinding.textFormSelectBankTitle.setTextColor(cwfsbTitleColor)
            mMaozyViewMaozyBinding.textFormSelectBankTitle.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwfsbTitleSize
            )

            mMaozyViewMaozyBinding.editFormSelectBank.setText(cwfsbText)
            mMaozyViewMaozyBinding.editFormSelectBank.setTextColor(cwfsbTextColor)
            mMaozyViewMaozyBinding.editFormSelectBank.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwfsbTextSize
            )

            mMaozyViewMaozyBinding.editFormSelectBank.hint = (cwfsbHint)
            mMaozyViewMaozyBinding.editFormSelectBank.setHintTextColor(cwfsbHintColor)

            mMaozyViewMaozyBinding.editFormSelectBank.addTextChangedListener(object :
                TextWatcher {
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun afterTextChanged(p0: Editable?) {
                    //判断是否显示删除按键
                    val tempContent = getMaozyContent()
                    //判断是否值已经变化
                    try {
                        if (tempContent.isNotBlank()) {
                            if (!mTempInputValue.equals(tempContent, false)) {
                                mTempInputValue = tempContent
                                recordMaozySelectMaozyEvent(tempContent)
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            })

            mMaozyViewMaozyBinding.vFormSelectBankAction.setOnClickListener {
                mOnActionClickListener.invoke(0)
            }

            //增加星号显示
            mMaozyViewMaozyBinding.textFormSelectBankTitle.addMaozyHighlightMaozyStyle(
                mutableListOf("*"),
                ContextCompat.getColor(context, R.color.main_red)
            )

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyContent(content: String) {
        mMaozyViewMaozyBinding.editFormSelectBank.setText(content)
        val splitStart = content.indexOf("#")
        if (splitStart >= 0) {
            val bankName = content.substring(0, splitStart)
            val bankCodeAndSplitChar = content.substring(splitStart)
            mMaozyViewMaozyBinding.editFormSelectBank.addMaozyTipsMaozyStyle(
                words = bankCodeAndSplitChar,
                sizeSpan = 1F,
                ContextCompat.getColor(context, R.color.transparent)
            )
        }
        if (content.isNotBlank()) {
            mMaozyViewMaozyBinding.textFormSelectBankErrTip.visibility = GONE
            mMaozyViewMaozyBinding.rlFormSelectBankContentBg.setBackgroundResource(R.drawable.maozy_form_input)
        }
        mMaozyFormMaozyConfig?.fillContent = content
        recordMaozySelectMaozyEvent(content.uppercase())
    }

    fun getMaozyContent(): String {
        return mMaozyViewMaozyBinding.editFormSelectBank.text.toString().trim()
    }

    fun getMaozyTitle(): String {
        return mMaozyViewMaozyBinding.textFormSelectBankTitle.text.toString().trim()
    }

    override fun setMaozyTitle(title: String) {
        cwfsbTitle = title
        mMaozyViewMaozyBinding.textFormSelectBankTitle.text = (cwfsbTitle)
        //增加星号显示
        mMaozyViewMaozyBinding.textFormSelectBankTitle.addMaozyHighlightMaozyStyle(
            mutableListOf("*"),
            ContextCompat.getColor(context, R.color.main_red)
        )
    }

    override fun setMaozyHint(hint: String) {
        cwfsbHint = hint
        mMaozyViewMaozyBinding.editFormSelectBank.hint = (cwfsbHint)
    }

    override fun isMaozyFormMaozyCompleted(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            mMaozyViewMaozyBinding.rlFormSelectBankContentBg.setBackgroundResource(R.drawable.maozy_form_input_error)
            mMaozyViewMaozyBinding.textFormSelectBankErrTip.visibility = VISIBLE
            return false
        }

        mMaozyViewMaozyBinding.textFormSelectBankErrTip.visibility = GONE
        mMaozyViewMaozyBinding.rlFormSelectBankContentBg.setBackgroundResource(R.drawable.maozy_form_input)
        return true
    }

    override fun isMaozyFormMaozyCompletedMaozySilent(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            return false
        }
        return true
    }

    override fun setMaozyFormMaozyConfig(item: RevObjInfoPageFieldConfig) {
        mMaozyFormMaozyConfig = item
        tag = item.paramName
        setMaozyTitle(item.title.toString())
        setMaozyHint(item.hint.toString())
        item.fillContent?.let { fillContent ->
            setMaozyContent(fillContent)
        }
        if (item.needCheckComplete == true) {
            isMaozyFormMaozyCompleted()
        }
    }

    override fun setMaozyOnMaozyStateMaozyChangedMaozyListener(listener: (Int, String) -> Unit) {
        mMaozyOnMaozyStateMaozyChangedMaozyListener = listener
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mOnActionClickListener = listener
    }

    override fun setMaozyDetail(detail: String) {
        setMaozyContent(detail)
    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }
}